// 这里的vm，就是我们在vue文件里面的this，所以我们能在这里获取vuex的变量，比如存放在里面的token
// 同时，我们也可以在此使用getApp().globalData，如果你把token放在getApp().globalData的话，也是可以使用的
import store from "../store"
const install = (Vue, vm) => {
	Vue.prototype.h5login=function(){
		return new Promise((resolve, reject) => {
			 this.$u.post('index.php?class=user&act=openid', {
			 				   openid:"oUkgM5Nc8DWFTsOGhxYLoPH111",
			 				   }).then(res => {
			 	  if (res.code==0){
					
					    if (res.data){
						   uni.setStorageSync('user', res.data);
						   uni.setStorageSync('uid', res.data.id);
						   
						  if (parseInt(res.data.status)!=2)
							  store.state.set.check_auth=1
							  else
							  store.state.set.check_auth=0
					    }
			 			resolve(res.data)
			 	  }else{
			 		  this.msg(res.msg)
			 	  }
			 
			 });
		})
	}
	// #ifdef MP
	Vue.prototype.wxlogin=function(){
		
		return new Promise((resolve, reject) => {
			 uni.login({
			   provider: 'weixin',
			   success:  (loginRes)=> {
			     this.$u.post('index.php?class=weixin&act=SessionKey', {code:loginRes.code}).then(res => {
			     	  if (res.code==0){
			 	          if (res.data.user){
			 				  uni.setStorageSync('user', res.data.user);
							   uni.setStorageSync('uid', res.data.user.id);
							  if (parseInt(res.data.user.status)!=2)
							      store.state.set.check_auth=1
								  else
								  store.state.set.check_auth=0
			 			  }
			     		  uni.setStorageSync('openid', res.data.openid);
						  resolve(res.data)
			     	  }else{
			     		  this.msg(res.msg)
			     	  }
			     
			     });
			   }
			 });
			 
		})
	}
	//#endif
	Vue.prototype.check_ysmsg = function() {
		//console.log(store.state.webset.msg.msgys,222)
		return new Promise((resolve, reject) => {
			uni.requestSubscribeMessage({
				tmplIds: store.state.set.webset.xcx_msgs,
				success(res) {
					//发送订阅消息
					Reflect.ownKeys(res).forEach(function(key) {
						if (res[key] == "accept") {
							resolve(1)
						}
					});
					resolve(0)
	
				}
			})
		})
	
	}
  	Vue.prototype.msg=function(title,duration=2000){
		uni.showToast({
				title:title,
				duration: duration,
					icon:"none"	
		})
	}
	Vue.prototype.jump=function(url){
		uni.navigateTo({
			url:url
		})
	}
	Vue.prototype.phone=function(phone){
		uni.makePhoneCall({
		    phoneNumber: phone //仅为示例
		});
	}
	Vue.prototype.copy=function(str){
		uni.setClipboardData({
			data: str,
			success: function () {
				vm.msg("复制成功")
			}
		});
	}
	Vue.prototype.jumpbar=function(url){
		uni.switchTab({
		    url: url
		});
	}
   Vue.prototype.arr=function(str){
     let temp= str.split(',');
     var that=this;
   	temp.forEach(function(val, index, arr){
   	      temp[index]=vm.xcx_url(temp[index])
   });
   	 
   	 return temp;
   }
   Vue.prototype.xcx_url=function(url){
	   
	  if (url.indexOf("http") != -1){
		  
		   return url;
	  }
	   else
	   return vm.rooturl+"/"+url;;
   }
	Vue.prototype.previewImage = function(urls,current) {
	
		uni.previewImage({
			urls: urls,
			current:current,
			longPressActions: {
				success: function(data) {
					//console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	}

}

export default {
	install
}
